$("#search_t").bind("click focus", function () {             
   var endtimeTf = $dp.$('search_d');             
   WdatePicker({                            
	   maxDate: '#F{$dp.$D(\'search_d\')}',      
	   dateFmt: "yyyy-MM-dd",              
	   onpicked: function () { endtimeTf.focus(); }          
		});         
   });          
$("#search_d").bind("click focus", function () {       
	WdatePicker({           
	   minDate: '#F{$dp.$D(\'search_t\')}', 
	   dateFmt: "yyyy-MM-dd"       
   });         
});
var Query = RichBase.extend({
	statics : {
		currentPage : 0,
		last : "",
		//oData : {},
		queryState : {
			loading : '<li class="loading" style="text-align:center; line-height:1; height:400px;"><p style="color:#aeaeae; font-size:12px; padding-top:300px;">加载数据...</p><img src="images/other/loading_1.gif" alt="" /></li>',
			empty : '<li class="loading" style="text-align:center; line-height:1; height:400px;"><p style="color:#aeaeae; font-size:12px; padding-top:300px;">没有数据...</p></li>',
			fail : '<li class="loading" style="text-align:center; line-height:1; height:400px;"><p style="color:#aeaeae; font-size:12px; padding-top:300px;">请求失败...</p></li>',
			unlogin : '<li class="loading" style="text-align:center; line-height:1; height:400px;"><p style="color:#aeaeae; font-size:12px; padding-top:300px;">登录过期，请重新登录...</p></li>',
			timeout : '<li class="loading" style="text-align:center; line-height:1; height:400px;"><p style="color:#aeaeae; font-size:12px; padding-top:300px;">网络请求超时...</p></li>',
			serverError : '<li class="loading" style="text-align:center; line-height:1; height:400px;"><p style="color:#aeaeae; font-size:12px; padding-top:300px;">请求出错...</p></li>'
		},
		isObjEmpty : function(obj){
			for(var i in obj){
				return false;
			}
			return true;
		}
	}, 
	EVENTS : {
		"click" : {
			"#pagenavW .pageBtn" : "onPrevNextClick"
		}
	},
	init : function(opt){
		this.prevBtn = $("#prevPageBtn");
		this.nextBtn = $("#nextPageBtn");
		function getQueryString(name) {
			var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
			var r = window.location.search.substr(1).match(reg);
			if (r != null) return unescape(r[2]); return null;
		}
		$(".info_url").attr("href",'userinfo.html?id='+getQueryString("?"))
		$("#did").val(getQueryString("?"));
		$(".credit_active").attr("aid",getQueryString("?"))
		
	},
	onPrevNextClick : function(that,e){
		var tarBtn = $(e.currentTarget);
		if($("#prevPageBtn").attr("data-first")!=0){
			that.prevBtn.removeClass("disable");
		}else{
			that.prevBtn.addClass("disable");
		}
		$(".page_num_").val("");
		if(tarBtn.hasClass("disable")) return false;
		var type = $(".page_sub").attr("data");
		//var curPage = that.statics.currentPage;
		var curPage = $(".page_num_").val();
		var toPage = tarBtn.hasClass("prevBtn") ? (curPage-1) : (curPage+1);
		//console.log("dddd"+curPage)
		var all = $(".nextBtn").attr("total");
		var aid = $(".credit_active").attr("aid");
		var mid = Number($("#prevPageBtn").attr("data-mid"));
		var last_ = Number($("#prevPageBtn").attr("data-last"));
		var first_ = Number($("#prevPageBtn").attr("data-first"));
		var total_ = Number($("#nextPageBtn").attr("total"));
		var bt = $("#search_t").val();
		var et = $("#search_d").val();
		var tyepp = $(".credit_active").attr("data");
		if(tyepp=="1"){
			var vy = 1;
		}else{
			var vy = 0;
		}
			if(tarBtn.hasClass("prevBtn")){
				that.nextBtn.removeClass("disable");
				if(first_=="0"){
					that.prevBtn.addClass("disable");
					return false;
				}
				else{
					if($(".pub_top_left").attr("data_li")=="2"){
						qQuery.query({
							last : first_,
							id : aid,
							bt : bt,
							et :et,
							type : vy,
							tyo : 2, //第一个是1  第二个是2
							dtype : 11, //第一个是4  第二个是11
						})	
					}else{
						qQuery.query({
							last : first_,
							id : aid,
							bt : bt,
							et :et,
							type : vy,
							tyo : 1, //第一个是1  第二个是2
							dtype : 4, //第一个是4  第二个是11
						})
					}	
				}

			}else{
				that.prevBtn.removeClass("disable");
				//console.log(last_);
				//console.log(total_);
				if(last_<=total_){
					if($(".pub_top_left").attr("data_li")=="2"){
						qQuery.query({
							last : last_,
							id : aid,
							bt : bt,
							et :et,
							type : vy,
							tyo : 2, //第一个是1  第二个是2
							dtype : 11, //第一个是4  第二个是11
						})	
					}else{
						qQuery.query({
							last : last_,
							id : aid,
							bt : bt,
							et :et,
							type : vy,
							tyo : 1, //第一个是1  第二个是2
							dtype : 4, //第一个是4  第二个是11
						})
					}	
				}else{
					that.nextBtn.addClass("disable");
				}
			}
			
	},
	query : function(opt){
		var tyepp = $(".credit_active").attr("data");
		if(tyepp=="1"){
			var vy = 1;
		}else{
			var vy = 0;
		}
		var that = this;
		var aid = $(".credit_active").attr("aid");
		var now = $(".search_titile").attr("now");
		var opt = opt || {};
		var pageSize = opt.pageSize || 15;
		var last = opt.last || "1";
		var currentPage = opt.last || "0";
		var sid = opt.sid;
		var pid = opt.pid;
		var tyo = opt.tyo || "1";
		var dtype = opt.dtype || "4";
		var bt = opt.bt ;
		var pa = opt.pa || "0"; 
		var et = opt.et || now;
		var data_li = opt.data_li;
		var ttimeout = opt.ttimeout || 20 * 60 * 1000;
		var data_li = $(".pub_top_left").attr("data_li");
		var listUl = $("#mlistUl");
		var pagenavW = $("#pagenavW");
		var status = this.statics.queryState;
		var data_a = {
				//pageSize : pageSize,
				currentPage : last,
				id : aid,
				bt : bt,
				et : et,
				type:vy,
				//tyo:tyo,
				dtype:dtype,
				act:"Query_Record"
				
			};
			PFT.Ajax({
				url : "api/inside/CreditRecord.php",
				type : "GET",
				dataType : "json",
				data : data_a,
				loading : function(){
					listUl.html(status["loading"]);
					pagenavW.hide();
					},
				removeLoading : function(res){
					that.statics.last = "";
					that.statics.currentPage += 1;
					listUl.html("");
					pagenavW.show();
				},
			},function(res){
				var lists = res.list;
					if(!that.statics.isObjEmpty(lists)){
						that.buildHtml(res,pa);
					}else{
						listUl.html(status["empty"]);
						$("#pagenavW").hide();
					}
			})
	},
	updata : function(opt){
		var tyepp = $(".credit_active").attr("data");
		if(tyepp=="1"){
			var vy = 1;
		}else{
			var vy = 0;
		}
		var that = this;
		var aid = $(".credit_active").attr("aid");
		var now = $(".search_titile").attr("now");
		var opt = opt || {};
		var pageSize = opt.pageSize || 15;
		var last = opt.last || "1";
		var currentPage = opt.last || "0";
		var sid = opt.sid;
		var pid = opt.pid;
		var tyo = opt.tyo || "1";
		var dtype = opt.dtype || "4";
		var bt = opt.bt ;
		var pa = opt.pa || "0"; 
		var et = opt.et || now;
		var data_li = opt.data_li;
		var ttimeout = opt.ttimeout || 20 * 60 * 1000;
		var data_li = $(".pub_top_left").attr("data_li");
		var listUl = $("#mlistUl");
		var pagenavW = $("#pagenavW");
		var status = this.statics.queryState;
		window.open('api/inside/CreditRecord.php?currentPage='+last+'&id='+aid+'&bt='+bt+'&et='+et+'&type='+vy+'&dtype='+dtype+'&act=Query_Record&loadExcel=1', '_blank');
	},
	buildHtml : function(res){
		var that = this;
		var lists = res.list;
		var str = "";
		var last = res.last;
		var data = lists;
		var total = res.total;
		var totalPage = res.totalPage || "0";
		var currentPage = res.currentPage;
		$(".nextBtn").attr("total",totalPage);
		$("#prevPageBtn").attr("data-mid",currentPage);
		$("#prevPageBtn").attr("data-last",currentPage+1);
		$("#prevPageBtn").attr("data-first",currentPage-1);
		$(".search_titile").attr("time",totalPage)
		str +=   '<thead>';
		str +=   '	<tr>';
		str +=   '		<th class="align_left">日期</th>';
		str +=   '		<th class="admin">操作员</th>';
		str +=   '		<th class="setwidth">操作说明</th>';
		str +=   '		<th class="setwidth_a">交易金额（元）</th>';
		str +=   '		<th class="note">备注</th>';
		str +=   '	</tr>';
		str +=   '</thead>';
		str +=   '<tbody>';
		for(var i in data){
			str +=   '<tr>';
			str +=   '	<td class="align_left">'+data[i]["rectime"]+'</td>';
			str +=   '	<td class="admin">'+data[i]["dname"]+'</td>';
			str +=   '	<td class="setwidth">'+data[i]["dreason"]+'</td>';
			//str +=   '	<td class="setwidth_a color_b">'+(data[i]["dmoney"] / 100).toFixed(2)+'</td>';
			str +=   '	<td class="setwidth_a color_b mone_" type="'+data[i]["add"]+'" add="'+data[i]["add"]+'" redu="'+data[i]["redu"]+'"></td>';
			str +=   '	<td class="note" title="'+data[i]["memo"]+'">'+data[i]["memo"]+'</td>';
			str +=   '</tr>';
		}
		str +=   '</tbody>';
		$("#mlistUl").html(str);
		$("html,body").animate({"scrollTop":0})
		$("#total_num").text("共"+totalPage+"页");	
		$("#whichPageNum").text(currentPage);
		$(".mone_").each(function(e){
			var tarBtn = $(e.currentTarget);
			if(($(this).attr("type"))=="undefined"){
				$(this).text("-"+$(this).attr("redu"))
				
				
			}else{
				$(this).text("+"+$(this).attr("add"))
				
			}
			
			
			
			
		})
	}
});
var qQuery = new Query({container:$(".credit_ti")});
var Main = RichBase.extend({
	statics : {},
	EVENTS : {
		"click" : {
			".pub_history" : "history_list",
			".pub_normal" : "normal_list",
			".page_sub" : "bl"
		}},
	init : function(opt){
		var that = this;
		this.container = opt.container;
		this.prevBtn = $("#prevPageBtn");
		this.nextBtn = $("#nextPageBtn");
		var bt = $("#search_t").val();
		var et = $("#search_d").val();
		qQuery.query({
			 bt:bt,
			 et:et
		});
	},
	bl : function(){
		//alert("ddd")
		//$(".page_sub").attr("data","1");
		var page_size = $(".page_num_").val();
		var all = $(".nextBtn").attr("total");
		var bt = $("#search_t").val();
		var et = $("#search_d").val();
		var data_li = $(".pub_top_left").attr("data_li");
		if(page_size<=all){
			if($(".pub_top_left").attr("data_li")=="2"){
					qQuery.query({
						data_li:data_li,
						bt : bt,
						et : et,
						tyo:2,
						dtype:11,
						last:page_size
					});
				}else{
					qQuery.query({
						data_li:data_li,
						bt : bt,
						et : et,
						tyo:1,
						dtype:4,
						last:page_size
					});
				}
		}
		else{
			return false;
		}
		
		
	},
	history_list : function(that,e){
			var target = $(e.currentTarget);
			var bt = $("#search_t").val();
			var et = $("#search_d").val();
			$(".pub_top_left").attr("data_li","2");
				qQuery.query({
					bt:bt,
					et:et,
					tyo:2,
					dtype:11
				})
			$("#mlistUl_wrapper").hide();
			$("#mlistUl_a_wrapper").show();
			target.addClass("nou");
			$(".pub_normal").removeClass("nou");
			 
			var pa = Number($(".pub_top_left").attr("data_b"));	
	},
	normal_list : function(that,e){
			var target = $(e.currentTarget);
			var bt = $("#search_t").val();
			var et = $("#search_d").val();
			$(".pub_top_left").attr("data_li","0");
			qQuery.query({
				bt:bt,
				et:et,
				tyo:1,
				dtype:4
			 })
			$(".pub_history").removeClass("nou");
			target.addClass("nou");
			$(".pub_top_left").attr("data_li","0");
			var pa = Number($(".pub_top_left").attr("data_a"));

	}

});
var search = RichBase.extend({
	statics : {},
	EVENTS : {
		"click" : {
			"#scope_btn" : "search_sub",
			".up_title" : "updata",
		}},
	init : function(opt){
		var that = this;
	},
	search_sub :function(){ 
		var bt = $("#search_t").val();
		var et = $("#search_d").val();
		var data_li = $(".pub_top_left").attr("data_li");
		
		if(!bt || !et){
			PFT_GLOBAL.U.Alert("fail",'<p style="width:240px">时间设定有误</p>');
			return false
		}else{
			if($(".pub_top_left").attr("data_li")=="2"){
				qQuery.query({
					data_li:data_li,
					bt : bt,
					et : et,
					tyo:2,
					dtype:11
				});
			}else{
				qQuery.query({
					data_li:data_li,
					bt : bt,
					et : et,
					tyo:1,
					dtype:4
				});
			}
		}
	},
	updata :function(){ 
		var bt = $("#search_t").val();
		var et = $("#search_d").val();
		var data_li = $(".pub_top_left").attr("data_li");
		 
		if(!bt || !et){
			PFT_GLOBAL.U.Alert("fail",'<p style="width:240px">时间设定有误</p>');
			return false
		}else{
			if($(".pub_top_left").attr("data_li")=="2"){
				qQuery.updata({
					data_li:data_li,
					bt : bt,
					et : et,
					tyo:2,
					dtype:11
				});
			}else{
				qQuery.updata({
					data_li:data_li,
					bt : bt,
					et : et,
					tyo:1,
					dtype:4
				});
			}
		}
	}

});
new Main({container:$(".credit_ti")});
new search({container:$(".credit_search")});




"nou");  //信用账户记录
			$(".pub_history").removeClass("nou");  //信用额度调整记录
	},
	history_list : function(that,e){
			var target = $(e.currentTarget);
			var bt = $("#search_t").val();
			var et = $("#search_d").val();
			$(".pub_top_left").attr("data_li","11"); //dtype   11 信用额度调整记录
			var dtype = $(".pub_top_left").attr("data_li");
				qQuery.query({
					bt:bt,
					et:et,
					dtype:dtype
				})
			$("#mlistUl_wrapper").hide();
			$("#mlistUl_a_wrapper").show();
			target.addClass("nou");
			$(".pub_normal").removeClass("nou");   //预存/还款记录
			$(".pub_record").removeClass("nou");  //信用账户记录
			$(".pub_trade").removeClass("nou");   //产品交易记录
	},
	normal_list : function(that,e){
			var target = $(e.currentTarget);
			var bt = $("#search_t").val();
			var et = $("#search_d").val();
			$(".pub_top_left").attr("data_li","4"); //dtype   4 预存/还款记录
			var dtype = $(".pub_top_left").attr("data_li");
			qQuery.query({
				bt:bt,
				et:et,
				dtype:dtype
			 })
			target.addClass("nou");
			$(".pub_record").removeClass("nou");  //信用账户记录
			$(".pub_history").removeClass("nou");  //信用额度调整记录
			$(".pub_trade").removeClass("nou");   //产品交易记录
	}

});
var search = RichBase.extend({
	statics : {},
	EVENTS : {
		"click" : {
			"#scope_btn" : "search_sub",
			".up_title" : "updata",
		}},
	init : function(opt){
		var that = this;
	},
	search_sub :function(){  //搜索
		var bt = $("#search_t").val();
		var et = $("#search_d").val();
		var data_li = $(".pub_top_left").attr("data_li");
		
		if(!bt || !et){
			PFT_GLOBAL.U.Alert("fail",'<p style="width:240px">时间设定有误</p>');
			return false
		}else{
			var dtype = $(".pub_top_left").attr("data_li");
			//console.log(dtype)
				qQuery.query({
					data_li:data_li,
					bt : bt,
					et : et,
					dtype:dtype
				});
		}
	},
	updata :function(){    //数据导出
		var bt = $("#search_t").val();
		var et = $("#search_d").val();
		var data_li = $(".pub_top_left").attr("data_li");
		 
		if(!bt || !et){
			PFT_GLOBAL.U.Alert("fail",'<p style="width:240px">时间设定有误</p>');
			return false
		}else{
				var dtype = $(".pub_top_left").attr("data_li");
				qQuery.updata({
					data_li:data_li,
					bt : bt,
					et : et,
					dtype:dtype
				});
		}
	}

});

var pa = Number($(".credit_active").attr("data"));
	if(pa=="1"){
		$(".credit_cei").text("分销商：");
		$(".host_money").show();
		$(".credit_partner").text("线下预存/还款");
		$(".credit_partner").addClass("xianxia");
		$(".credit_partner").attr("onclick",null);
	}else{
		$(".credit_cei").text("供应商：");
		$(".host_money").hide();
		$(".credit_partner").text("预存/还款充值");
		$(".credit_partner").removeClass("xianxia");
		$(".credit_partner").attr("onclick","setFund(65,this)");
	
	}
new Main({container:$(".credit_ti")});
new search({container:$(".credit_search")});




